var me = this;
define(['dojo/_base/declare', 'geomap/plugins/layers/Echarts3Layer'], function (
    declare,
    Echarts3Layer
) {
    var MigrationMapLayer = declare([Echarts3Layer], {
        constructor: function (options) {
            this._map = options.map;

            var echarts = options.echarts;

            var mouseEnabled = options.isNeedClick || options.mouseEnabled || false;

            var n = (this._echartsContainer = document.createElement('div'));

            (n.style.position = 'absolute'),
                (n.style.height = this._map.height + 'px'),
                (n.style.width = this._map.width + 'px'),
                (n.style.top = 0),
                (n.style.left = 0);

            if (this._map && this._map.__container) {
                var __container = this._map.__container;
                if (__container.childNodes && __container.childNodes.length > 0) {
                    var childNode = __container.childNodes[0];
                    if (mouseEnabled) {
                        childNode.appendChild(n);
                    } else {
                        childNode.insertBefore(n, childNode.getElementsByTagName('svg')[0]);
                    }
                }
            }
            this._init(this._map, echarts);
        },
        setData: function (mapView, layer) {
            var _this = this;
            _this._map = this._map || mapView.getMap();

            var o = layer.layer || {};
            this.options = o;
            var data = $.extend(true, [], o.data);
            var options = o.options;

            var color = options.color || ['#a6c84c', '#ffa022', '#46bee9'];
            var planePath =
                options.planePath ||
                'path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.063c0.521-84.662-26.629-121.796-63.961-121.491c-37.332-0.305-64.482,36.829-63.961,121.491l0.073,208.063l-319.9,221.799v89.254l330.343-157.288l12.238,241.308l-134.449,92.931l0.531,42.034l175.125-42.917l175.125,42.917l0.531-42.034l-134.449-92.931l12.238-241.308L1705.06,1318.313z';
            //汽车
            //'path://M4560,4996.6c-530.4-68.2-932.1-239.8-1201.2-514.8c-156-159.9-508.9-803.4-598.6-1092c-50.7-161.9-52.7-384.2-11.7-840.5c39-421.2,42.9-386.1-54.6-432.9c-117-56.5-208.7-138.4-241.8-216.4c-23.4-58.5-23.4-72.2,7.8-136.5c44.8-93.6,93.6-99.4,263.2-31.2l134.6,54.6l58.5-58.5l58.5-58.5l-48.8-536.3C2822.5-3.2,2808.9-360.1,2846-1095.2c9.7-218.4,5.8-386.1-19.5-663c-66.3-723.5-2-1550.3,159.9-2039.7c72.1-216.4,157.9-366.6,278.8-487.5c87.8-89.7,142.3-122.9,304.2-189.2c349.1-146.3,805.3-257.4,1232.4-302.3c173.6-17.5,269.1-17.5,438.8,0c659.1,74.1,1347.5,298.3,1544.4,505.1c140.4,146.3,255.4,399.7,329.6,733.2c101.4,440.7,134.6,1197.3,80,1758.9c-42.9,432.9-46.8,838.5-11.7,982.8c40.9,165.8,9.8,733.2-107.2,1959.8l-50.7,524.6l58.5,56.6l56.6,56.6l150.1-62.4c175.5-74.1,228.1-64.3,265.2,48.8c39,122.9-42.9,235.9-251.6,341.3l-85.8,44.8l13.6,157.9c7.8,87.8,21.4,261.3,31.2,384.2c25.4,300.3,23.4,503.1-1.9,627.9c-29.3,128.7-397.8,863.9-514.8,1019.9c-222.3,298.3-563.6,489.5-1066.6,596.7C5470.7,5004.4,4797.9,5025.8,4560,4996.6z M5517.5,3073.9c444.6-76.1,811.2-255.5,1051.1-516.8c195-208.7,189.1-152.1,85.8-906.7c-48.8-358.8-93.6-657.2-97.5-661.1c-5.9-5.8-117,11.7-247.7,39c-544,111.2-762.4,134.6-1269.4,134.6c-522.6,2-717.6-19.5-1271.4-128.7c-169.6-33.1-312-56.5-315.9-52.6c-3.9,3.9-48.8,304.2-99.4,664.9l-89.7,657.2l48.8,85.8c56.5,93.6,232,273,356.9,362.7c255.4,183.3,663,313.9,1127.1,360.8C4936.4,3126.5,5338.1,3105.1,5517.5,3073.9z M6443.7-2179.4v-702l-70.2-76.1c-187.2-210.6-573.3-321.8-1197.3-347.1c-717.6-29.2-1302.6,101.4-1519.1,335.4l-80,87.8v692.3c0,380.2,7.8,692.2,15.6,692.2c9.8,0,81.9-17.5,161.9-37c319.8-81.9,618.1-107.3,1236.3-107.3c631.8,0,920.4,25.4,1255.8,113.1c91.6,25.3,173.6,46.8,183.3,48.8C6437.9-1479.4,6443.7-1793.3,6443.7-2179.4z';
            var planeSize = options.planeSize || 15;
            var planeColor = options.planeColor;
            var planePeriod = options.planePeriod || 6;

            var effectSize = options.effectSize || 3;
            var effectColor = options.effectColor || '#fff';
            var effectTrailLength = options.effectTrailLength || 0.7;
            var effectPeriod = options.effectPeriod || 6;

            var lineWidth = options.lineWidth || 2;
            var lineType = options.lineType || 'solid';

            var legend = options.legend || {};
            var legendData = legend.data || [];

            if (options.defaultEffectScatterSize == undefined) {
                _this._defaultEffectScatterSize = 10;
            } else {
                _this._defaultEffectScatterSize = options.defaultEffectScatterSize;
            }
            if (options.defaultEffectScatterSize_max == undefined) {
                _this._defaultEffectScatterSize_max = 50;
            } else {
                _this._defaultEffectScatterSize_max = options.defaultEffectScatterSize_max;
            }
            _this._codeFieldName = options.codeFieldName || 'code';
            _this._nameFieldName = options.nameFieldName || 'name';
            _this._lngFieldName =
                options.lngFieldName || mapView.getDefaultSettings().lngFieldName || 'x';
            _this._latFieldName =
                options.latFieldName || mapView.getDefaultSettings().latFieldName || 'y';
            _this._valueFieldName = options.valueFieldName || 'value';
            _this._formatterEffectScatterTooltip_single =
                options.formatterSingleCallback || options.formatterEffectScatterTooltip_single;
            _this._formatterEffectScatterTooltip_multi =
                options.formatterMultiCallback || options.formatterEffectScatterTooltip_multi;
            _this._formatterEffectScatterLabel_single = options.formatterEffectScatterLabel_single;
            _this._formatterEffectScatterLabel_multi = options.formatterEffectScatterLabel_multi;
            _this._formatterLinesLabel =
                options.formatterLinesCallback || options.formatterLinesLabel;
            _this._lineLabelShow = options.lineLabelShow;
            _this._symbolSizeWeight = 8; //图标大小权重值
            var chartsContainer = _this.getEchartsContainer();

            var myChart = _this.initECharts(chartsContainer);

            var convertData = function (item) {
                var from = item.from;
                var to = item.to;
                var list = [];
                if (_this.isArray(from)) {
                    for (var i = 0; i < from.length; i++) {
                        var o = Object.assign({}, from[i]);
                        o.coords = [
                            [from[i].x, from[i].y],
                            [to.x, to.y],
                        ];
                        o.value = from[i].value;
                        list.push(o);
                    }
                } else if (_this.isArray(to)) {
                    for (var i = 0; i < to.length; i++) {
                        var o = Object.assign({}, to[i]);
                        o.coords = [
                            [from.x, from.y],
                            [to[i].x, to[i].y],
                        ];
                        o.value = to[i].value;
                        list.push(o);
                    }
                }
                return list;
            };
            var series = [];
            data.forEach(function (item, i) {
                var name = '';
                if (legendData && legendData[i]) {
                    name = legendData[i];
                }
                if (
                    !item.from ||
                    (_this.isArray(item.from) && item.from.length < 1) ||
                    (typeof item.from == 'object' && JSON.stringify(item.from) == '{}')
                ) {
                    console.log('缺少数据!');
                    return;
                }
                if (
                    !item.to ||
                    (_this.isArray(item.to) && item.to.length < 1) ||
                    (typeof item.to == 'object' && JSON.stringify(item.to) == '{}')
                ) {
                    console.log('缺少数据!');
                    return;
                }
                name = name || (item.from && item.from.name) || (item.to && item.to.name);

                var cData = convertData(item);

                var multiData = null;
                var singleData = null;
                if (item.from.length > 0) {
                    multiData = item.from;
                    singleData = [item.to];
                } else {
                    multiData = item.to;
                    singleData = [item.from];
                }
                series.push(
                    {
                        name: name,
                        type: 'lines',
                        zlevel: 1,
                        effect: {
                            show: true,
                            period: effectPeriod,
                            trailLength: effectTrailLength,
                            color: effectColor,
                            symbolSize: effectSize,
                        },
                        lineStyle: {
                            normal: {
                                color: color[i],
                                width: 0,
                                curveness: 0.2,
                            },
                        },
                        data: cData,
                    },
                    {
                        name: name,
                        type: 'lines',
                        zlevel: 2,
                        effect: {
                            show: true,
                            period: planePeriod,
                            trailLength: 0,
                            symbol: planePath,
                            symbolSize: planeSize,
                            color: planeColor,
                        },
                        lineStyle: {
                            normal: {
                                color: color[i],
                                width: lineWidth,
                                type: lineType,
                                opacity: 0.4,
                                curveness: 0.2,
                            },
                        },
                        data: cData,
                        itemStyle: {
                            normal: {
                                label: {
                                    show: _this._lineLabelShow,
                                    position: '',
                                    formatter: function (e) {
                                        var html = e.value;
                                        if (_this._formatterLinesLabel) {
                                            html = _this._formatterLinesLabel(e);
                                        }
                                        return html;
                                    },
                                },
                            },
                        },
                    },
                    {
                        name: name,
                        type: 'effectScatter',
                        coordinateSystem: 'geo',
                        zlevel: 2,
                        rippleEffect: {
                            brushType: 'stroke',
                        },
                        label: {
                            normal: {
                                show: true,
                                position: 'right',
                                formatter: function (e) {
                                    var html = e.name;
                                    if (_this._formatterEffectScatterLabel_multi) {
                                        html = _this._formatterEffectScatterLabel_multi(e);
                                    }
                                    return html;
                                },
                            },
                        },
                        symbolSize: function (val) {
                            var symbolSize = _this._defaultEffectScatterSize;
                            if (val[2] && _this._symbolSizeWeight > 0) {
                                symbolSize = val[2] / _this._symbolSizeWeight;
                            }
                            if (symbolSize < _this._defaultEffectScatterSize) {
                                symbolSize = _this._defaultEffectScatterSize;
                            }
                            if (symbolSize > _this._defaultEffectScatterSize_max) {
                                symbolSize = _this._defaultEffectScatterSize_max;
                            }
                            return symbolSize;
                        },
                        itemStyle: {
                            normal: {
                                color: function (item) {
                                    return (item.data && item.data.color) || color[i];
                                },
                            },
                        },
                        tooltip: {
                            formatter: function (e) {
                                var data = e.data;
                                var html = `<div>${name}</div>
                                             <div style="display: flex;align-items:center;">
                                                <div style="background:${color[i]};width:6px;height:6px;border-radius: 50%;margin-right:3px;"></div>
                                                <div>${data.name}：</div>
                                                <div>${data.value[2]}</div>
                                             </div>`;
                                if (
                                    typeof _this._formatterEffectScatterTooltip_multi == 'function'
                                ) {
                                    html = _this._formatterEffectScatterTooltip_multi(data, name);
                                }
                                return html;
                            },
                        },
                        data: multiData.map(function (dataItem) {
                            return Object.assign({}, dataItem, {
                                name: dataItem.name,
                                value: [dataItem.x, dataItem.y, dataItem.value],
                            });
                        }),
                    },
                    {
                        name: name,
                        type: 'effectScatter',
                        coordinateSystem: 'geo',
                        zlevel: 2,
                        rippleEffect: {
                            brushType: 'stroke',
                        },
                        label: {
                            normal: {
                                show: true,
                                position: 'right',
                                formatter: function (e) {
                                    var html = e.name;
                                    if (_this._formatterEffectScatterLabel_single) {
                                        html = _this._formatterEffectScatterLabel_single(e);
                                    }
                                    return html;
                                },
                            },
                        },
                        symbolSize: function (val) {
                            var symbolSize = _this._defaultEffectScatterSize;
                            if (val[2] && _this._symbolSizeWeight > 0) {
                                symbolSize = val[2] / _this._symbolSizeWeight;
                            }
                            if (symbolSize < _this._defaultEffectScatterSize) {
                                symbolSize = _this._defaultEffectScatterSize;
                            }
                            if (symbolSize > _this._defaultEffectScatterSize_max) {
                                symbolSize = _this._defaultEffectScatterSize_max;
                            }
                            return symbolSize;
                        },
                        itemStyle: {
                            normal: {
                                color: singleData[0].color || color[i],
                            },
                        },
                        tooltip: {
                            formatter: function (e) {
                                var data = e.data;
                                var html = `<div>${data.name}</div>`;
                                if (
                                    typeof _this._formatterEffectScatterTooltip_single == 'function'
                                ) {
                                    html = _this._formatterEffectScatterTooltip_single(data);
                                }
                                return html;
                            },
                        },
                        data: singleData.map(function (dataItem) {
                            return Object.assign({}, dataItem, {
                                name: dataItem[_this._nameFieldName],
                                code: dataItem[_this._codeFieldName],
                                value: [
                                    dataItem[_this._lngFieldName],
                                    dataItem[_this._latFieldName],
                                    dataItem[_this._valueFieldName],
                                ],
                            });
                        }),
                    }
                );
            });

            var option = {
                title: {
                    text: '',
                    subtext: '',
                    left: 'center',
                    textStyle: {
                        color: '#fff',
                    },
                },
                tooltip: {
                    trigger: 'item',
                },
                legend: {
                    orient: 'vertical',
                    bottom: '50',
                    left: 'right',
                    data: legendData,
                    textStyle: {
                        color: '#fff',
                    },
                    selectedMode: 'single',
                },
                geo: {
                    map: '',
                    label: {
                        emphasis: {
                            show: false,
                        },
                    },
                    roam: true,
                    itemStyle: {
                        normal: {
                            areaColor: '#323c48',
                            borderColor: '#404a59',
                        },
                        emphasis: {
                            areaColor: '#2a333d',
                        },
                    },
                },
                series: series,
            };

            _this.setOption(option);

            _this.setScale(this._map, this._echartsContainer);
            if (typeof options.callback == 'function') {
                options.callback(layer);
            }
        },
    });
    return MigrationMapLayer;
});
